/**
 *  Copyright (C) 2008 Richard Hall
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the 
 *  GNU General Public License as published by the Free Software Foundation; either version 3 of the 
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, 
 *  see <http://www.gnu.org/licenses>.
 *
 *  Additional permission under GNU GPL version 3 section 7
 *
 *  If you modify this Program, or any covered work, by linking or combining it with [name of library] 
 *  (or a modified version of that library), containing parts covered by the terms of 
 *  [name of library's license], the licensors of this Program grant you additional permission to 
 *  convey the resulting work. {Corresponding Source for a non-source form of such a combination shall 
 *  include the source code for the parts of [name of library] used as well as that of the 
 *  covered work.}
 *
 * 
 */

package com.gpsmedia.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * This class is used for Calendar operations that are frequently used. It deals
 * with accounting for all devices (camera, audio device and gps device) having
 * different times. It assumes that the gps device is most accurate and that it
 * records the data in the UTC time zone (which is expected with Nmea data).
 * 
 * @author Richard Hall
 */
public class CalendarData {

    long realPicDifference;
    long realAudioDifference;
    TimeZone outputTimeZone;

    /**
	 * Constructor for this class
	 * 
	 * @param realFirstPicUTC
	 *            The time of the first pic in UTC
	 * @param realFirstAudioUTC
	 *            The time of the first audio in UTC
	 * @param firstPicDevice
	 *            The time of the first pic, the device time
	 * @param firstAudioDevice
	 *            The time of the first audio, the device time
	 * @param outputTimeZone
	 *            The output time zone
	 */
    public CalendarData(Calendar realFirstPicUTC, Calendar realFirstAudioUTC,
            Calendar firstPicDevice, Calendar firstAudioDevice, TimeZone outputTimeZone) {
        realPicDifference = realFirstPicUTC.getTimeInMillis() - firstPicDevice.getTimeInMillis();
        realAudioDifference = realFirstAudioUTC.getTimeInMillis() - firstAudioDevice.getTimeInMillis();
        this.outputTimeZone = outputTimeZone;
    }

    /**
	 * Returns the corrected picture time
	 * 
	 * @param originalTime
	 *            The original time
	 * @return The correct time
	 */
    public Calendar getCorrectPictureDate(Calendar originalTime) {

        originalTime.add(Calendar.MILLISECOND, (int) realPicDifference);
        return originalTime;
    }

    /**
	 * Returns the corrected audio time
	 * 
	 * @param originalTime
	 *            The original time
	 * @return The corrected time
	 */
    public Calendar getCorrectAudioDate(Calendar originalTime) {
        originalTime.add(Calendar.MILLISECOND, (int) realAudioDifference);
        return originalTime;
    }

    /**
	 * Compares a picture date with the UTC date in the log file. Will return 0
	 * if they are the same, or 1 or -1 to indicate in which direction they are
	 * out.
	 * 
	 * @param pictureDate
	 *            The picture date
	 * @param logDate
	 *            The gps log date
	 * @return 0,1, -1
	 */
    public int comparePictureDates(Calendar pictureDate, Calendar logDate) {
        Calendar tempDate = Calendar.getInstance();
        tempDate.clear();
        tempDate = (Calendar) pictureDate.clone();

        tempDate = getCorrectPictureDate(tempDate);
        return logDate.compareTo(tempDate);
    }

    /**
	 * Compares a audio date with the UTC date in the log file. Will return 0
	 * if they are the same, or 1 or -1 to indicate in which direction they are
	 * out.
	 * 
	 * @param audioDate
	 *            The audio date
	 * @param logDate
	 *            The gps log date
	 * @return 0,1, -1
	 */
    public int compareAudioDates(Calendar audioDate, Calendar logDate) {
        Calendar tempDate = Calendar.getInstance();
        tempDate.clear();
        tempDate = (Calendar) audioDate.clone();
        tempDate = getCorrectAudioDate(tempDate);
        return logDate.compareTo(tempDate);
    }

    /**
	 * Gets the actual time for the time zone for the gps log
	 * 
	 * @param date
	 *            The date to be changed
	 * @return The changed date
	 */
    public Calendar getActualTimeZoneDate(Calendar date) {

        date.add(Calendar.MILLISECOND, outputTimeZone.getOffset(date.getTimeInMillis()));
        return date;

    }
}
